///创建一个路由器
import { createRouter, createWebHashHistory } from 'vue-router';
///学习主页
import LearnIndex from "../views/LearnIndex.vue";
///
import Index from "../views/Index.vue";
import TrainingLearnIndex from "../views/TrainingLearnIndex.vue";
import TrainingLearnTaskIndex from "../views/TrainingLearnTaskIndex.vue";
import LearnTaskNavigation from "../views/LearnTaskNaavigation.vue";
import IndexFirstBtnNavigation from "../components/IndexFirstBtnNavigation.vue";
import LearnCareerNavigation from "../components/learn/LearnCareerNavigation.vue";
import LearnNavigationItem from "../components/learn/LearnLifeNavigation.vue";
import ManageIndex from "../views/ManageIndex.vue";
import practiceIndex from "../views/practiceIndex.vue";
import Examination from "@/views/Examination.vue";

const router = createRouter({
  history: createWebHashHistory(),
  ///主页初始化
  routes: [
    {
      path: '/',
      component: Index,
      name: 'home',
      meta: {
        defaultParams: {
          mainTopIndexNavigation: '0',///主菜单的Index
          leftMenuIndex: 0,///左边的Index下标
          yearCycleIndex: 0,///年度周期性培训情况的Index
          weaknessAnalysis: 0,///弱项分析的Index
          trendTraining: 0,///趋势培训
        }
      }
    },
    ///培训个人职业生命学习
    ///params[indexNavigationId] 左边导航区域的Index;
    ///params[indexNavigationId]
    {
      path: '/LearnIndex',
      name: 'learnLife',
      component: LearnIndex,
      children: [{
        path: 'learnCareerLeftNavigation',
        component: LearnNavigationItem,
        name: 'learnLifeLeftMenu'
      }]
    },
    ///培训任务学习
    {
      path: '/TrainingLearnIndex',
      name: 'learnTask',
      component: TrainingLearnIndex,
      children: [
        {
          path: "learnTaskNavigation",
          component: LearnCareerNavigation,
          name: 'learnTaskNavigationLeftMenu',
        }
      ],
    },
    ////TrainingLearnTaskIndex/learnNavigation
    ///课件资源
    {
      path: '/TrainingLearnTaskIndex',
      component: TrainingLearnTaskIndex,
      name: 'CoursewareResources',
      children: [
        {
          path: "learnNavigation",
          component: LearnTaskNavigation,
          name: 'CoursewareResourcesLeftMenu'
        }
      ],
      meta: {
        keepAlive: true   //true为需要缓存，false为不需要缓存（不需要的也可以不加）
      }
    },
    {
      path: '/practice', //练
      component: () => practiceIndex,
      children: [
        {
          path: "practiceNavigation",
          component: LearnNavigationItem,
          name: 'practiceNavigation'
        }
      ]
    },
    ///题库资源
    {
      path: '/QuestionBankResource',
      component: () => import("@/views/resource-management/theory/index.vue"),
      children: [
        {
          path: "learnNavigation",
          component: IndexFirstBtnNavigation,
          name: 'learnNavigationLeftMenu'
        },
        // {
        //   path: "add",
        //   component: () => import("@/views/resource-management/theory/addEditSubject.vue"),
        //   name: "TheoryAdd",
        //   meta: {
        //     title: "新增题目",
        //   },
        // },
      ],
    },
    ///管
    {
      path: '/manager',
      component: ManageIndex,
      children: [
        {
          path: "managerNavigation",
          component: LearnTaskNavigation,
          name: 'managerNavigationLeftMenu'
        }
      ]
    },
      ///考
    {
      path: '/examination',
      component:Examination,
      name:'examination',
      children:[
        {
          path:'examinationNavigation',
          component:LearnNavigationItem,
          name: 'examinationNavigationLeftMenu'
        }
      ]
    },
    {
      path: "/QuestionBankResource/add",
      component: () => import("@/views/resource-management/theory/addEditSubject.vue"),
      name: "TheoryAdd",
      meta: {
        title: "新增题目",
      },
    },
    {
      path: "/QuestionBankResource/edit",
      component: () => import("@/views/resource-management/theory/addEditSubject.vue"),
      name: "TheoryEdit",
      meta: {
        title: "修改题目",
      },
    },
    {
      path: "/QuestionBankResource/detail",
      component: () => import("@/views/resource-management/theory/detailSubject.vue"),
      name: "TheoryDetail",
      meta: {
        title: "题目详情",
      },
    },
    {
      path: "/regulations",
      component: () => import("@/views/resource-management/regulations/index.vue"),
      name: "",
      meta: {
        title: "规章管理",
      },
    },
    {
      path: "/regulations/detail",
      component: () => import("@/views/resource-management/regulations/detail.vue"),
      name: "",
      meta: {
        title: "规章管理",
      },
    },
    {
      path: "/regulations/detail/subject/add",
      component: () => import("@/views/resource-management/theory/addEditSubject.vue"),
      name: "RegulationsTheoryAdd",
      meta: {
        title: "新增题目",
      },
    },
  ]
},);

// 在全局前置守卫中设置默认参数
router.beforeEach((to, from, next) => {
  if (to.meta && to.meta.defaultParams) {
    for (const [key, value] of Object.entries(to.meta.defaultParams)) {
      if (!to.query[key]) {
        // 如果参数未定义，则设置默认值
        next({ ...to, query: { ...to.query, [key]: value } });
        return;
      }
    }
  }
  next(); // 继续导航
});

export default router;